package com.iwhalecloud.citybrain.flow.platform.manager.application.service;

import java.util.List;

import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.MessageSenderContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.properties.MessageSenderProperties;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2020/12/2 上午9:27
 */
@Service
@EnableConfigurationProperties(MessageSenderProperties.class)
public class MessageSenderManager {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private List<MessageSender> messageSenders;

    public void send(MessageSenderContext context) {
        if (CollectionUtils.isEmpty(messageSenders)) {
            logger.info("未配置消息发送器");
            return;
        }
        for (MessageSender sender : messageSenders) {
            if (!sender.isEnable()) {
                continue;
            }
            try {
                sender.sendMessage(context);
            } catch (Exception e) {
                logger.error("{}发送失败,失败信息：{}", sender.getDescription(), e.getCause().toString());
            }
        }
    }
}
